Geocomputation Apply

R
Geocomputation
Author

sungil_park

Published

April 29, 2023

Modified

April 29, 2023

Library packages

rm(list = ls())

library(sf)
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.0     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rgdal)
필요한 패키지를 로딩중입니다: sp
Please note that rgdal will be retired during 2023,
plan transition to sf/stars/terra functions using GDAL and PROJ
at your earliest convenience.
See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
rgdal: version: 1.6-6, (SVN revision 1201)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.5.2, released 2022/09/02
Path to GDAL shared files: C:/Users/sungi/AppData/Local/R/win-library/4.2/rgdal/gdal
GDAL binary built with GEOS: TRUE 
Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
Path to PROJ shared files: C:/Users/sungi/AppData/Local/R/win-library/4.2/rgdal/proj
PROJ CDN enabled: FALSE
Linking to sp version:1.6-0
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(plotly)

다음의 패키지를 부착합니다: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(ggtext)
library(tmap)

Load data

Following data is geographic data for Chungcheongnam-do.

http://data.nsdi.go.kr/dataset/15145

EMD_CD EMD_NM SGG_OID COL_ADM_SE GID geometry
읍면동 코드 읍면동 이름 시군구 코드 시 코드 GID 지리정보

Convert data type (sp -> sf)

data_sp <- readOGR("C:/trainsets_2/LSMD_ADM_SECT_UMD_충남/LSMD_ADM_SECT_UMD_44.shp",encoding = "euc-kr")
Warning: OGR support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
Warning: GDAL support is provided by the sf and terra packages among others
Warning: GDAL support is provided by the sf and terra packages among others

Warning: GDAL support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
Warning: OGR support is provided by the sf and terra packages among others
OGR data source with driver: ESRI Shapefile 
Source: "C:\trainsets_2\LSMD_ADM_SECT_UMD_충남\LSMD_ADM_SECT_UMD_44.shp", layer: "LSMD_ADM_SECT_UMD_44"
with 316 features
It has 5 fields
Warning: GDAL support is provided by the sf and terra packages among others
Warning: GDAL support is provided by the sf and terra packages among others

Warning: GDAL support is provided by the sf and terra packages among others
data_sf = st_as_sf(data_sp)
data_sf %>% head()
Simple feature collection with 6 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 911558.7 ymin: 1829456 xmax: 934941.7 ymax: 1853055
Projected CRS: Korea 2000 / Unified CS
    EMD_CD EMD_NM SGG_OID COL_ADM_SE GID                       geometry
0 44800360 결성면    3817      44800 979 MULTIPOLYGON (((911891.8 18...
1 44800350 은하면    3816      44800 980 MULTIPOLYGON (((920373.2 18...
2 44800340 장곡면    3815      44800 981 MULTIPOLYGON (((931934 1838...
3 44800330 홍동면    3814      44800 982 MULTIPOLYGON (((931821 1843...
4 44800320 금마면    3813      44800 983 MULTIPOLYGON (((931724.3 18...
5 44800256 홍북읍    3812      44800 984 MULTIPOLYGON (((931724.3 18...
data_sf %>% ggplot(aes(fill = COL_ADM_SE))+
  geom_sf()+
  theme_minimal()+
  labs(title = "충청남도")

Filter Place of Interest

cheonan <- data_sf %>% 
  filter(COL_ADM_SE=="44130")

cheonan_seobuk <- cheonan %>%  
  filter(substr(EMD_CD,1,5)=="44133")

cheonan_dongnam <- cheonan %>%  
  filter(substr(EMD_CD,1,5)=="44131")
cheonan %>% ggplot(aes(fill=substr(EMD_CD,1,5)))+
  geom_sf()+
  theme_minimal()+
  labs(title = "천안시 (구별)")+
  scale_fill_discrete(name = "구",
                      labels = c("동남구","서북구"))

chsb <- cheonan_seobuk %>% ggplot(aes(fill=EMD_NM))+
  geom_sf()+
  geom_sf_text(mapping = aes(label = EMD_NM))+
  labs(title = "천안시 서북구 (읍면동)")+
  theme_minimal()+
  scale_fill_discrete(name = "읍면동")

chsb %>% ggplotly()
chdn <- cheonan_dongnam %>% ggplot(aes(fill=EMD_NM))+
  geom_sf()+
  geom_sf_text(mapping = aes(label = EMD_NM))+
  labs(title = "천안시 동남구 (읍면동)")+
  theme_minimal()+
  scale_fill_discrete(name = "읍면동")

chdn %>% ggplotly()

건축년도

data <- read.csv("C:/trainsets_2/GEOCOMPS.csv")
data_sf <- data %>% st_as_sf(coords = c("Longitude","Latitude"))
st_crs(data_sf) <- 4737
st_crs(data_sf)
Coordinate Reference System:
  User input: EPSG:4737 
  wkt:
GEOGCRS["Korea 2000",
    DATUM["Geocentric datum of Korea",
        ELLIPSOID["GRS 1980",6378137,298.257222101,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    USAGE[
        SCOPE["Horizontal component of 3D system."],
        AREA["Republic of Korea (South Korea) - onshore and offshore."],
        BBOX[28.6,122.71,40.27,134.28]],
    ID["EPSG",4737]]
cheonan <- cheonan %>% 
  filter(EMD_NM!="광덕면")

gn <- ggplot()+
  geom_sf(data = cheonan,fill=NA)+
  geom_sf(data = data_sf,mapping = aes(color = 건축년도))+
  scale_color_gradient(low = "blue", high = "red")+
  theme_minimal()

gn %>% ggplotly()

아파트별 평단가

ggplotly

data_nm_raw <- read.csv("C:/Users/sungi/Documents/linear_model_dataset_R.csv")
data_nm_raw <- data_nm_raw %>% mutate(평단가 = 거래금액/전용면적)

data_nm <- data_nm_raw %>% 
  group_by(아파트) %>% 
  summarize(평균거래액 = mean(평단가) %>% as.integer(),
            Latitude = mean(Latitude),
            Longitude = mean(Longitude)) %>% 
  mutate(정보 = paste(아파트,평균거래액,sep = ", 평단가 :"))

data_nm_sf <- data_nm %>% st_as_sf(coords = c("Longitude","Latitude"))
st_crs(data_nm_sf) <- 4737

cheonan <- cheonan %>% 
  filter(EMD_NM!="광덕면")

  
gn <- ggplot()+
  geom_sf(data = cheonan,color = "black")+
  geom_sf(data = data_nm_sf,aes(fill = 정보))+
  theme_minimal()+
  theme(legend.position = "none")

gn %>% ggplotly()

Tmap library

tmap_mode("view")
tmap mode set to interactive viewing
tmap_options(check.and.fix = TRUE)

tm_shape(cheonan["EMD_NM"])+
  tm_polygons(col="white",alpha = 0.3)+
  tm_shape(data_nm_sf)+
  tm_symbols(shape = 2, col = "red", size = 0.05)+
  tm_layout(main.title = "High points in New Zealand", main.title.size = 1,
            bg.color = "lightblue")
Warning: The shape cheonan["EMD_NM"] is invalid (after reprojection). See
sf::st_is_valid
Symbol shapes other than circles or icons are not supported in view mode.